﻿using System;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Utils;

namespace Xenta.Operations
{
    /// <summary>
    /// Creates a sales rule.
    /// </summary>
    public class CreateFixedTaxRule : CreateSalesRule<FixedTaxRuleEntity>
    {
        #region Properties

        /// <summary>
        /// Gets or sets the amount.
        /// </summary>
        public decimal Amount
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public FixedTaxRuleFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override Err Validate(IUnitOfWork uow)
        {
            if(Amount <= Decimal.Zero)
                return Err.Of<FixedTaxRuleEntity>().NotValid("Amount");
            return base.Validate(uow);
        }

        protected override void Construct(IUnitOfWork uow, FixedTaxRuleEntity entity)
        {
            base.Construct(uow, entity);

            entity.Amount = Amount;
            entity.Flags = Flags;
        }

        #endregion
    }

    /// <summary>
    /// Updates the sales rule.
    /// </summary>
    public class UpdateFixedTaxRule : UpdateSalesRule<FixedTaxRuleEntity>
    {
        #region Properties

        /// <summary>
        /// Gets or sets the amount.
        /// </summary>
        public decimal Amount
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public FixedTaxRuleFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override Err Validate(IUnitOfWork uow)
        {
            if(Amount <= Decimal.Zero)
                return Err.Of<FixedTaxRuleEntity>().NotValid("Amount");
            return base.Validate(uow);
        }

        protected override void Revise(IUnitOfWork uow, FixedTaxRuleEntity entity)
        {
            base.Revise(uow, entity);

            entity.Amount = Amount;
            entity.Flags = Flags;
        }

        #endregion
    }
}
